import json
from fastapi import Depends, APIRouter
from pydantic import BaseModel

from routes.slot_management import sql_polars,sqlpyodbc
from util.JWT import get_current_user


inventoryQuery_slot = APIRouter()

# 刷新
class queryType(BaseModel):
    loction: str
    flag: int

@inventoryQuery_slot.post("/inventoryquery")
async def inventoryquery(data:queryType,id: dict = Depends(get_current_user)):
    sql=""
    if data.flag==0:
        sql=f"""
        SELECT 
    A.LocationID,
	B.ContractNo,
	B.CaseNo,
	B.CN_Name,
	B.PackageType_C
FROM 
    (SELECT LocationID FROM StorageLocation) AS A
LEFT JOIN 
    (SELECT C.LocationID, C.ContractNo,D.CN_Name,C.PackageType_C, C.CaseNo FROM CaseResult AS C 
	LEFT JOIN Country AS D ON C.CountryID=D.CountryID WHERE isout = 0) AS B
ON 
    A.LocationID = B.LocationID
ORDER BY A.LocationID,B.ContractNo
        """
    else:
        sql=f"""
        SELECT 
    A.LocationID,
	B.ContractNo,
	B.CaseNo,
	B.CN_Name,
	B.PackageType_C
FROM 
    (SELECT LocationID FROM StorageLocation) AS A
LEFT JOIN 
    (SELECT C.LocationID, C.ContractNo,D.CN_Name,C.PackageType_C, C.CaseNo FROM CaseResult AS C 
	LEFT JOIN Country AS D ON C.CountryID=D.CountryID WHERE isout = 0) AS B
ON 
    A.LocationID = B.LocationID WHERE A.LocationID='{data.loction}'
ORDER BY A.LocationID,B.ContractNo
        """
    df = sql_polars.query(sql)
    if df.height < 1:
        return {
            "code": 204,
            "status": "no",
            "msg": "没有内容",
            "data": []
        }
    df = df.to_dicts()
    json_string = json.dumps(df, ensure_ascii=False)
    return {
        "code": 200,
        "status": "ok",
        "msg": "success",
        "data": json_string
    }
